<?php 


class Banners{
	
	public static $msgs = array();
	var $userid = '';
		
	function __construct()
	{
		$this->user_id = $_SESSION['admin_id'];
	}
	
	/* 
	***  Public Functions
	*/
	
	 public function addBanner($data)
	 {
	 	global $db, $msgError, $config;		
		require_once($config['basedir'].'/include/classes/upload.class.php');
		clean_array($data, 'banner_code');
		$path = $config['basedir']."/images/banners/";
		$upload = new Upload();
		$upload->allowed_extensions = "gif,jpg,jpeg,png";
		$upload->directory = $path;
		$upload->max_filesize = 1024*1024;
		$upload->upload_type = "file";
		$upload->upload_file = $_FILES['banner_image'];
		if (is_uploaded_file($_FILES['banner_image']['tmp_name']))
		{        
				$file_name_1 = $this->getFileBasename($_FILES['banner_image']['name'])."_".time();
				$upload->out_filename = $file_name_1;
				$upload->upload_process();
				if( $upload->is_error() )
				{
					$upload_errors = $upload->errors;
					foreach($upload_errors as $err)
					{	
						$this->msgs[] = $err;		
					}
				}else{
						$filename = $upload->saved_filename;
					   	
				}
		}
		else
		{
			$this->msgs[] = 'Please upload banner image.';
		}
		
		if(($data['banner_name']==''))
		{
		     $this->msgs[] = 'Banner name can not be empty.';
		}
		
		if( empty($this->msgs) )
		{
			 $record = array(
			               		'banner_name' => strip_tags($data['banner_name']),
								'banner_link' => $data['banner_link'],
								'banner_image' => $filename,
								'status' => 1
							);
		 
			 if( $db->insert("banners", $record) ) {
			 
				redirect("admin/banners.php?added=1");		
			 }
		}
		
		$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}

		
	 }
	 
	function getBanners($data)
	{
		
		global $db, $pager, $config;
		$pager = new Paginator();
		if( isset($data['banner_name']) && $data['banner_name'] != '')
		{
			$where = " where banner_name like '%" . $db->escape($data['banner_name']) . "%'";
		}
		
		if( isset($data['new_page_limit']) && intval($data['new_page_limit']) > 0 ){
			$config['page_limit'] = $data['new_page_limit'];
		}
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}
		$sql = "SELECT SQL_CALC_FOUND_ROWS b.* 
				FROM banners as b $where order by banner_name  $limit" ;
		
         $result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
	}
	
	function getBannerById($id)
	{       
		    global $db, $pager, $config;
		    $pager = new Paginator();
		    $sql = "SELECT * FROM banners WHERE banner_id = ".intval($id)."";
            $result = $db->first($sql);
		    return $result;
	}

	 public function updateBanner($data)
	 {
		global $db, $msgError, $config;
		require_once($config['basedir'].'/include/classes/upload.class.php');
		clean_array($data, 'banner_code');
		$path = $config['basedir']."/images/banners/";
		$upload = new Upload();
		$upload->allowed_extensions = "gif,jpg,jpeg,png";
		$upload->directory = $path;
		$upload->max_filesize = 1024*1024;
		$upload->upload_type = "file";
		$upload->upload_file = $_FILES['banner_image'];
		$banner_id = $data['banner_id']; 
		
		if(($data['banner_name']==''))
		{
		     $this->msgs[] = 'Banner name can not be empty.';
		}
		
		$banner_data = $this->getBannerById($banner_id);
		$new_image_upload = false;		
		if (is_uploaded_file($_FILES['banner_image']['tmp_name']))
		{  
			$file_name_1 = $this->getFileBasename($_FILES['banner_image']['name'])."_".time();
			$upload->out_filename = $file_name_1;	
			$upload->upload_process();
			if( $upload->is_error() )
			{
				$upload_errors = $upload->errors;
				foreach($upload_errors as $err)
				{	
					$this->msgs[] = $err;		
				}
			}else{ 
				  $new_image_upload = true;
				   $filename = $upload->saved_filename;
				   $old_banner_image = $data['old_banner_image'];
				   @unlink($path.$old_banner_image);
		   }
		}
		else
		{
		    $filename  = $data['old_banner_image'];
		}
		
		if( $filename=='' )
		{
		     $this->msgs[] = 'Banner image can not be empty.';
		}
			
		if( empty($this->msgs) )
		{	
			
			$record = array(
			               		'banner_name' => strip_tags($data['banner_name']),
								'banner_link' => $data['banner_link']
							); 
			if( $new_image_upload )	{
				$record['banner_image'] = $filename;
			}				
							
			$where = array( "banner_id" => $banner_id );	
		    if( $db->update("banners", $record, $where) ){
			    redirect("admin/banners.php?updated=1");
			}	
			
		}
		
			$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}
				
	}
	
	function deleteBanner($banner_id)
	{
		global $db, $msgError, $config;
		$banner_id = intval($banner_id);
		require_once($config['basedir'].'/include/classes/upload.class.php');
		$path = $config['basedir']."/images/banners/";
		$data = $db->first("SELECT banner_image FROM banners WHERE banner_id = ".intval($banner_id));
		$banner_image = $data['banner_image'];
		$where = array( 'banner_id' => $banner_id );
		if( $db->delete("banners", $where) )
		{ 
		       unlink($path.$banner_image);
			   redirect("admin/banners.php?deleted=1");	
		}else{
				$this->msgs[] = 'Some error occurs. Banner can not be deleted.';
		}
         		
		
		
	}


	public function changeStatus($data)
	{    
	  global $db;
	  $record = array('status' => intval($data['status']));
      $where = array( "banner_id" => intval($data['banner_id']) );
	  if( $db->update("banners", $record, $where) )
	  {  
		echo 1;
	  }
   	}
  
  	function getFileBasename($file_name) 
	{
	    $file = substr($file_name, 0,strrpos($file_name,'.'));   
	    return $file;
	}
   
	//=============================================
}
?>